
//层序遍历从下到上，只要每次添加的时候在头部插就好

class Solution5 {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>>  list=new ArrayList<>();
             if(root==null){
            return list;
        }
       
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> ret = new ArrayList<>();
            while (size != 0) {
                TreeNode cur = queue.poll();
                System.out.println(cur.val + "");
                ret.add(cur.val);
                size--;
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }

            }
            list.add(0,ret);

        }
        return list;

        }

    
}